<?php

require_once 'config/config.php';

$username = $_POST['username'];
$password = $_POST['password'];

$location = (isset($_POST['location'])&&$_POST['location'] != ""?$_POST['location']:'0/0');
$seeing = $_POST['seeing'];
$transparency = $_POST['transparency'];
$groupnorth = $_POST['groupnorth'];
$spotnorth = $_POST['spotnorth'];
$groupsouth = $_POST['groupsouth'];
$spotsouth = $_POST['spotsouth'];

$telescoop = $_POST['telescoop'];
$correction = $_POST['correction'];

// Check of de gebruikers juiste credentials heeft
$query = 'SELECT user_id, password FROM users WHERE username = "'.$username.'"';
$result = mysql_query($query);

if(mysql_num_rows($result) == 1) {
    
    $line = mysql_fetch_assoc($result);
    $user_id = $line['user_id'];
    $user_password = $line['password'];
    
    if(crypt($password, $user_password) === $user_password) {
        $query = 'SELECT t.telescope_ID FROM telescopes t
            INNER JOIN users_telescopes ut ON (ut.telescope_ID = t.telescope_ID)
            WHERE ut.user_ID = "'.$user_id.'"
            AND t.name = "'.$telescoop.'"';
        $result = mysql_query($query); 

        if(mysql_num_rows($result) > 0){

            $line = mysql_fetch_assoc($result);
            $telescoop_id = $line['telescope_ID'];

            $location = explode('/',$location);

            $longitude = trim($location[0]);
            $latitude = trim($location[1]);

            $spottotal = $spotnorth + $spotsouth;
            $grouptotal = $groupnorth + $groupsouth;
            
            $wolff = 10*$grouptotal+$spottotal;

            $query = 'INSERT INTO observations (telescope_id, user_id, observation_date, observation_longitude, observation_latitude, seeing, transparency, groupnorth, spotnorth, groupsouth, spotsouth, grouptotal, spottotal, correction, wolff) 
                VALUES ("'.$telescoop_id.'", "'.$user_id.'",now(), "'.$longitude.'", "'.$latitude.'", "'.$seeing.'","'.$transparency.'","'.$groupnorth.'","'.$spotnorth.'","'.$groupsouth.'","'.$spotsouth.'","'.$grouptotal.'","'.$spottotal.'","'.$correction.'", "'.$wolff.'")';

            $result = mysql_query($query);

            echo 'OBSERVATIONADDED';
            exit;

        }
        
    }
    
    
}

echo 'OBSERVATIONFAILED';


?>
